#include <iostream>
#include <string>
#include <unordered_set>

using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if(s.size() == 0) return 0;

        unordered_set<char> lookup;
        int maxStr = 0;
        int left = 0;

        for (int i = 0; i < s.size(); i++){

            while(lookup.find(s[i]) != lookup.end()) {
                lookup.erase(s[left]);
                left++;
            }
            maxStr = max(maxStr,i-left+1);
            lookup.insert(s[i]);
        }

        return maxStr;
    }
};

int main(void)
{
    Solution newSolution;
    int      len = 0;

    string s1 = "abcabcbb";
    len = newSolution.lengthOfLongestSubstring(s1);
    if (len == 3) {
        cout << "test case 1: "<< "abcabcbb" << " OK!" << endl;
    }
    cout << "len= " << len << endl;

    string s2 = "bbbbb";
    len = newSolution.lengthOfLongestSubstring(s2);
    if (len == 1) {
        cout << "test case 2: "<< "bbbbb" << " len=" << len << " OK!" << endl;
    }
    cout << "len= " << len << endl;

    string s3 = "pwwkew";
    len = newSolution.lengthOfLongestSubstring(s3);
    if (len == 3) {
        cout << "test case 3: "<< "pwwkew" << " len=" << len << " OK!" << endl;
    }
    cout << "len= " << len << endl;

    string s4 = "ynyo";
    len = newSolution.lengthOfLongestSubstring(s4);
    if (len == 3) {
        cout << "test case 4: "<< "ynyo" << " len=" << len << " OK!" << endl;
    }
    cout << "len= " << len << endl;

    
    string s5 = "ohvhjdml";
    len = newSolution.lengthOfLongestSubstring(s5);
    if (len == 5) {
        cout << "test case 5: "<< "ohvhjdml" << " len=" << len << " OK!" << endl;
    }
    cout << "len= " << len << endl;

    string s6 = "abcabcbbfnml";
    len = newSolution.lengthOfLongestSubstring(s6);
    if (len == 5) {
        cout << "test case 6: "<< "abcabcbb" << " OK!" << endl;
    }
    cout << "len= " << len << endl;

    return 0;
}

// g++ -g -Wall -std=c++11 3_find_substring_without_repeating.cpp

